/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.punish.config.dto.ConfigOrgDecideBasisInfoDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigOrgDecideBasisInfoQueryDto;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigOrgDecideBasisInfoMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigOrgDecideBasisInfo;
import com.icinfo.cloud.provider.punish.config.service.IConfigOrgDecideBasisInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.PartyTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 描述: 机构判定依据配置表 config_org_decide_basis_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年07月08日
 */
@Slf4j
@Service
public class ConfigOrgDecideBasisInfoServiceImpl extends MyBatisServiceSupport implements IConfigOrgDecideBasisInfoService {

    @Resource
    private ConfigOrgDecideBasisInfoMapper configOrgDecideBasisInfoMapper;
    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;

    /**
     * 描述： 保存部门判定依据配置信息
     *
     * @author: zhengqiang
     * @date: 2022/7/11
     * @param: saveDto
     * @return {@link Result< String>}
     **/
    @Override
    public Result<String> saveDeptDecideBasisConfigInfo(ConfigOrgDecideBasisInfoDto saveDto) {
        //校验参数
        if (saveDto.getDecideBasisKinds().contains("罚款")) {
            if (ObjectUtils.isEmpty(saveDto.getPunishMoneyPersonLimit())
                    || ObjectUtils.isEmpty(saveDto.getPunishMoneyUnitLimit())) {
                return Result.error("判定依据种类包含罚款时,罚款个人额度或单位额度不允许为空");
            }

        }
        if (saveDto.getDecideBasisKinds().contains("没收违法所得")) {
            if (ObjectUtils.isEmpty(saveDto.getConfiscateIllegalMoneyPersonLimit())
                    || ObjectUtils.isEmpty(saveDto.getConfiscateIllegalMoneyUnitLimit())) {
                return Result.error("判定依据种类包含没收违法所得时,没收违法所得个人额度或单位额度不允许为空");
            }
        }
        if (saveDto.getDecideBasisKinds().contains("没收非法财务")) {
            if (ObjectUtils.isEmpty(saveDto.getConfiscateIllegalPropertyValuePersonLimit())
                    || ObjectUtils.isEmpty(saveDto.getConfiscateIllegalPropertyValueUnitLimit())) {
                return Result.error("判定依据种类包含没收非法财务时,没收非法财务个人额度或单位额度不允许为空");
            }
        }

        //操作成功记录数
        int count = 0;

        ConfigOrgDecideBasisInfo info = new ConfigOrgDecideBasisInfo();
        BeanUtils.copyProperties(saveDto, info);

        ConfigOrgDecideBasisInfoQueryDto queryDto = new ConfigOrgDecideBasisInfoQueryDto();
        queryDto.setOrgCode(saveDto.getOrgCode());
        queryDto.setDecideBasisType(saveDto.getDecideBasisType());
        ConfigOrgDecideBasisInfo orgDecideBasisInfo = getOrgDecideBasisInfo(queryDto);
        if (ObjectUtils.isEmpty(orgDecideBasisInfo)) {
            //不存在则新增
            info.setId(BaseUtil.createUid());
            count = configOrgDecideBasisInfoMapper.insertSelective(info);
        } else {
            info.setId(orgDecideBasisInfo.getId());
            info.setUpdateTime(LocalDateTime.now());
            count = configOrgDecideBasisInfoMapper.updateByPrimaryKeySelective(info);
        }

        if (count > 0) {
            return Result.success("判定依据更新成功");
        } else {
            return Result.error("判定依据更新失败");
        }

    }

    /**
     * 描述： 查询部门判定依据配置信息
     *
     * @author: zhengqiang
     * @date: 2022/7/11
     * @param: queryDto
     * @return {@link ConfigOrgDecideBasisInfo}
     **/
    @Override
    public ConfigOrgDecideBasisInfo getOrgDecideBasisInfo(ConfigOrgDecideBasisInfoQueryDto queryDto) {

        Example example = new Example(ConfigOrgDecideBasisInfo.class);
        example.createCriteria().andEqualTo("orgCode", queryDto.getOrgCode())
                .andEqualTo("decideBasisType", queryDto.getDecideBasisType());
        List<ConfigOrgDecideBasisInfo> configOrgDecideBasisInfos = configOrgDecideBasisInfoMapper.selectByExample(example);
        if (!ObjectUtils.isEmpty(configOrgDecideBasisInfos)) {
            return configOrgDecideBasisInfos.get(0);
        }
        return null;
    }

    /**
     * 描述： 校验案件是否符合机构配置判定依据
     *
     * @author: zhengqiang
     * @date: 2022/7/11
     * @param: caseId 案件id
     * @param: decideBasisType 判定依据类型
     * @param: punishKindsList 案件处罚种类列表
     * @param: draftPunishMoney 处罚金额
     * @param: confiscateIllegalMoney 没收违法所得
     * @param: partyType 当事人类型
     * @return {@link String} 0:否;1是
     **/
    @Override
    public String checkIsNeedByConfigOrgDecideBasis(String caseId, String decideBasisType, List<String> punishKindsList, String draftPunishMoney, String confiscateIllegalMoney, String partyType, String confiscateIllegalPropertyValue) {
        String isNeed = YesOrNoEnum.NO.getCode();

        //案件处罚种类列表为空,返回否
        if (ObjectUtils.isEmpty(punishKindsList)) {
            return isNeed;
        }
        //查询案件主表
        CaseMainInfo caseMainInfo = caseMainInfoMapper.selectByPrimaryKey(caseId);
        //案件不存在
        if (ObjectUtils.isEmpty(caseMainInfo)) {
            return isNeed;
        }
        //查询机构判定依据配置
        ConfigOrgDecideBasisInfoQueryDto queryDto = new ConfigOrgDecideBasisInfoQueryDto();
        queryDto.setOrgCode(caseMainInfo.getCaseOrgCode());
        queryDto.setDecideBasisType(decideBasisType);
        ConfigOrgDecideBasisInfo orgDecideBasisInfo = getOrgDecideBasisInfo(queryDto);
        if (ObjectUtils.isEmpty(orgDecideBasisInfo)) {
            return isNeed;
        }

        //获取机构集体议案判定依据的处罚种类
        List<String> decideBasisKindsList = Arrays.asList(orgDecideBasisInfo.getDecideBasisKinds().split(","));

        for (String punishKind : punishKindsList) {
            //判定依据中不包含该处罚种类则下一条
            if (!decideBasisKindsList.contains(punishKind)) {
                continue;
            }

            if ("罚款".equals(punishKind)) {
                //判断是否符合罚款额度
                if (ObjectUtils.isEmpty(draftPunishMoney)) {
                    isNeed = YesOrNoEnum.NO.getCode();
                    continue;
                }
                BigDecimal punishMoney = new BigDecimal(draftPunishMoney);
                if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
                    BigDecimal punishMoneyPersonLimit = new BigDecimal(orgDecideBasisInfo.getPunishMoneyPersonLimit());
                    if (punishMoney.compareTo(punishMoneyPersonLimit) >=0) {
                        isNeed = YesOrNoEnum.YES.getCode();
                        break;
                    }
                } else if (PartyTypeEnum.LEGAL.getCode().equals(partyType)) {
                    BigDecimal punishMoneyUnitLimit = new BigDecimal(orgDecideBasisInfo.getPunishMoneyUnitLimit());
                    if (punishMoney.compareTo(punishMoneyUnitLimit) >= 0) {
                        isNeed = YesOrNoEnum.YES.getCode();
                        break;
                    }
                }
            }
            else if ("没收违法所得".equals(punishKind)) {
                //判断是否符合没收违法所得额度
                if (ObjectUtils.isEmpty(confiscateIllegalMoney)) {
                    isNeed = YesOrNoEnum.NO.getCode();
                    continue;
                }
                BigDecimal illegalMoney = new BigDecimal(confiscateIllegalMoney);
                if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
                    BigDecimal confiscateIllegalMoneyPersonLimit = new BigDecimal(orgDecideBasisInfo.getConfiscateIllegalMoneyPersonLimit());
                    if (illegalMoney.compareTo(confiscateIllegalMoneyPersonLimit) >= 0) {
                        isNeed = YesOrNoEnum.YES.getCode();
                        break;
                    }
                } else if (PartyTypeEnum.LEGAL.getCode().equals(partyType)) {
                    BigDecimal confiscateIllegalMoneyUnitLimit = new BigDecimal(orgDecideBasisInfo.getConfiscateIllegalMoneyUnitLimit());
                    if (illegalMoney.compareTo(confiscateIllegalMoneyUnitLimit) >= 0) {
                        isNeed = YesOrNoEnum.YES.getCode();
                        break;
                    }
                }
            }
            else if (Constant.STRING_2.equals(decideBasisType) && "没收非法财物".equals(punishKind)) {
                //判断是否符合没收非法财务额度
                if (ObjectUtils.isEmpty(confiscateIllegalPropertyValue)) {
                    isNeed = YesOrNoEnum.NO.getCode();
                    continue;
                }
                BigDecimal illegalPropertyValue = new BigDecimal(confiscateIllegalPropertyValue);
                if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
                    BigDecimal confiscateIllegalPropertyValuePersonLimit = new BigDecimal(orgDecideBasisInfo.getConfiscateIllegalPropertyValuePersonLimit());
                    if (illegalPropertyValue.compareTo(confiscateIllegalPropertyValuePersonLimit) >= 0) {
                        isNeed = YesOrNoEnum.YES.getCode();
                        break;
                    }
                } else if (PartyTypeEnum.LEGAL.getCode().equals(partyType)) {
                    BigDecimal confiscateIllegalPropertyValueUnitLimit = new BigDecimal(orgDecideBasisInfo.getConfiscateIllegalPropertyValueUnitLimit());
                    if (illegalPropertyValue.compareTo(confiscateIllegalPropertyValueUnitLimit) >= 0) {
                        isNeed = YesOrNoEnum.YES.getCode();
                        break;
                    }
                }
            }
            //isNeed = YesOrNoEnum.YES.getCode();
            //判定依据中包含该处罚种类的任意一种
            else if (decideBasisKindsList.contains(punishKind)) {
                isNeed = YesOrNoEnum.YES.getCode();
                break;
            }
        }

        return isNeed;
    }
}
